package cn.dt70.controller;

import cn.dt70.pojo.AqbUser;
import cn.dt70.service.AqbBankcardService;
import cn.dt70.service.AqbNewprojectService;
import cn.dt70.utils.Constants;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

@Controller
@RequestMapping("/newGift/")
public class NewGiftController {

    private static final Timer timer = new Timer();

    @Resource
    private AqbNewprojectService aqbNewprojectService;
    @Resource
    private AqbBankcardService aqbBankcardService;

    @RequestMapping("toTiyan")
    public String toTiyan(){

        return "pre/tiyanjin";
    }

    @RequestMapping("begin")
    @ResponseBody
    public String newGiftBegin(HttpSession session){
        String backMsg ="F";
        Integer userId = ((AqbUser)(session.getAttribute(Constants.USER_SESSION))).getUserId();
        if (aqbNewprojectService.findNewGiftLogByUserId(userId)){//没有体验过的新用户
            if (aqbNewprojectService.addNewGiftLog(userId)){
                System.out.println("体验金数据库新增记录！");
            }

            Calendar calendar = Calendar.getInstance();

            Date nowTime = calendar.getTime();
            //派息两天
            //每天15：00之前体验，次日10：00第一笔利息到账;
            //每天15：00之后体验，后天10：00第一笔利息到账;
            calendar.set(Calendar.HOUR_OF_DAY, 10);
            calendar.set(Calendar.MINUTE, 0);
            calendar.set(Calendar.SECOND, 0);

            SimpleDateFormat smt = new SimpleDateFormat("HH:mm:ss");

            String[] times = smt.format(nowTime).split(":");
            if (Integer.parseInt(times[0])<11){
                calendar.add(Calendar.DAY_OF_MONTH,1);
            }else {
                calendar.add(Calendar.DAY_OF_MONTH,2);
            }

            Date firstTime = calendar.getTime();

            System.out.println("开始时间："+firstTime);
            timer.scheduleAtFixedRate(new GiftPerday(userId),firstTime,24*60*60*1000);

            backMsg="T";
        }

        return JSON.toJSONString(backMsg);
    }

    //定时派息
    class GiftPerday extends TimerTask {

        private Integer userId;

        public Integer getUserId() {
            return userId;
        }

        public void setUserId(Integer userId) {
            this.userId = userId;
        }

        public GiftPerday(){}

        public GiftPerday(Integer userId){
            this.userId=userId;
        }

        private int count=0;

        @Override
        public void run() {
            count++;
            System.out.println("第"+count+"次进来");
//            aqbNewprojectService.modifyBalance(new BigDecimal("11.22"),userId);
            aqbBankcardService.modifyBankMoney(userId.toString(),11.22);

            if (count>=2){
                this.cancel();
            }
        }
    }

}
